package com.zhoug.demo.viewpermission.page;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.zhoug.common.utils.StringUtils;
import com.zhoug.demo.viewpermission.R;
import com.zhoug.demo.viewpermission.network.PageMenu;
import com.zhoug.viewpermission.NavigationMenu;
import com.zhoug.zmui.dialog.ZMUIAbsBottomSheetDialogFragment;

/**
 * @author: zhoug
 * @since: 2025-06-12
 */
public class PageMenuInfoFragment extends ZMUIAbsBottomSheetDialogFragment {
    private NavigationMenu mData;
    private TextView mTvTitle;
    private TextView mTvInfo;

    public PageMenuInfoFragment(NavigationMenu data) {
        this.mData = data;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_page_menu_info;
    }

    @Override
    protected void init(View root) {
        findViews(root);

        mTvInfo.setText(buildInfo());

    }


    private CharSequence buildInfo() {
        if (mData != null) {
            String info = new InfoBuilder()
                    .append("类型", getMenuTypeName(mData.getMenuType()))
                    .append("名称", mData.getName())
                    .append("编码", mData.getCode())
                    .append("描述", mData.getDescription())
                    .append("组件路径", mData.getPath())
                    .append("路由路径", mData.getNavigation())
                    .append("权限标识", mData.getPermissionKey())
                    .append("默认图标", mData.getIcon())
                    .append("排序号", mData.getSortOrder() + "")
                    .append("显示", mData.isVisible() ? "显示" : "隐藏")
                    .append("状态", mData.getStatus() == 0 ? "正常" : "停用")
//                    .append("是否删除", mData.isDeleted() ? "是" : "否")
                    .build();
            SpannableString span=new SpannableString(info);
            int index = info.indexOf("隐藏");
            if(index!=-1){
                span.setSpan(new ForegroundColorSpan(Color.RED),index,index+2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            index = info.indexOf("停用");
            if(index!=-1){
                span.setSpan(new ForegroundColorSpan(Color.RED),index,index+2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            return span;
        }
        return null;
    }




    private static String getMenuTypeName(int menuType){
        return switch (menuType){
            case 0->"菜单";
            case 1->"目录";
            case 2->"按钮";
            default -> "未知";
        };
    }

    private static class InfoBuilder {
        private final StringBuilder mStringBuilder = new StringBuilder();

        public InfoBuilder() {
        }

        public InfoBuilder append(String name, String value) {
            mStringBuilder.append(name);
            mStringBuilder.append(":\u3000\u3000");
            if(value!=null){
                mStringBuilder.append(value);
            }
            mStringBuilder.append("\n");
            return this;
        }

        public String build() {
            return mStringBuilder.toString();
        }
    }


    private void findViews(View view) {
        mTvTitle = view.findViewById(R.id.tv_title);
        mTvInfo = view.findViewById(R.id.tv_info);
    }
}
